import router from "./router";
import store from './store'

import {getToken, setToken} from "./utils/token";
import {Message} from "element-ui";
router.beforeEach(async (to, from, next) => {
    const hasToken = getToken();
    //如果有token
    if (hasToken){
        if (to.path === '/login'){
            next({ path: '/Welcome' })
        }
        else {
            const role = store.getters.role;
            if (role && role.length > 0){
                next();
            }else {
                await store.dispatch('getUserInfo',getToken());
                //await store.dispatch('generateRoutes'); //前端生成路由
                await store.dispatch('serverGenerateRoutes');
                next()
            }
        }
    }
    else {
        if (to.path === '/login'){
            next()
        }
        else {
            next('/login')
        }
    }
});